package com.zy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.bean.po.RuleComponent;
import com.zy.bean.qo.SystemCmpQo;
import com.zy.bean.vo.SystemCmpVo;
import com.zy.enums.ReportTypeEnum;
import com.zy.mapper.RuleComponentMapper;
import com.zy.service.RuleComponentService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hanhuafeng
 * @createDate 2023-07-17 18:36:00
 * @description 规则组件库
 */
@Service
public class RuleComponentServiceImpl extends ServiceImpl<RuleComponentMapper, RuleComponent>
        implements RuleComponentService {

    /**
     * 根据上报类型查询组件
     *
     * @param reportType 上报类型
     * @return java.util.List<com.zy.bean.po.RuleComponent>
     */
    @Override
    public List<RuleComponent> listByReportType(ReportTypeEnum reportType) {
        return this.lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(reportType), RuleComponent::getReportType, reportType.getCode())
                .list();
    }

    /**
     * 查询全部组件
     *
     * @return java.util.List<com.zy.bean.po.RuleComponent>
     */
    @Override
    public Map<String, List<RuleComponent>> listAll() {
        List<RuleComponent> ruleComponents = this.list();
        if (ruleComponents.isEmpty()) {
            return new HashMap<>(0);
        }
        return ruleComponents.stream().collect(Collectors.groupingBy(RuleComponent::getCmpType));
    }

    /**
     * 分页查询系统组件
     *
     * @param qo 查询条件
     * @return 返回系统组件
     */
    @Override
    public IPage<SystemCmpVo> listSystemComponent(SystemCmpQo qo) {
        IPage<SystemCmpVo> result = new Page<>(qo.getCurrent(), qo.getSize());

        LambdaQueryWrapper<RuleComponent> wrapper = new LambdaQueryWrapper<>();
        IPage<RuleComponent> page = this.page(qo.getPage(), wrapper);
        if (page.getRecords().isEmpty()) {
            result.setRecords(new LinkedList<>());
            return result;
        }
        List<SystemCmpVo> systemCmpVos = page.getRecords().stream().map(SystemCmpVo::trance).toList();
        result.setRecords(systemCmpVos);
        result.setTotal(page.getTotal());
        return result;
    }
}




